import { reqGetGoodDetailInfoData } from "@/api";

const state = {
  goodDetailInfo: {},
};
const actions = {
  // 1. 获取商品详情数据
  async getGoodDetailInfoData({ commit }, id) {
    const result = await reqGetGoodDetailInfoData(id);
    if (result.code === 200) {
      commit("SAVE_GOOD_DETAIL_INFO_DATA", result.data);
    } else {
      console.log("111");
      console.log(result.message);
    }
  },
};
const mutations = {
  // 1. 存储详情数据
  SAVE_GOOD_DETAIL_INFO_DATA(state, goodDetailInfo) {
    state.goodDetailInfo = goodDetailInfo;
  },
  // 2.更改放大镜图片的默认图片链接
  UPDATE_IMG_DEFAULT_URL(state, imgUrl) {
    state.goodDetailInfo.skuInfo.skuDefaultImg = imgUrl;
  },
};
const getters = {
  categoryView(state) {
    return state.goodDetailInfo.categoryView || {};
  },
  skuInfo(state) {
    return state.goodDetailInfo.skuInfo || {};
  },
  spuSaleAttrList(state) {
    return state.goodDetailInfo.spuSaleAttrList || [];
  },
};

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
};
